package ge.telasi.tasks.ui;

import de.javasoft.plaf.synthetica.SyntheticaLookAndFeel;
import de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.UIManager;

/**
 * @author dimitri
 */
public class Launcher {
    private static MainFrame mainFrame;
    private static TrayIcon trayIcon;
    private static NotificationWindow notificationWindow;

    static private void commonInitialization() {
        initLookAndFeel();
        mainFrame = initMainFrame();
        trayIcon = initSystemTray();
        notificationWindow = new NotificationWindow();
        validateTrayIcon();
        clearTempDir();
    }

    static protected void launchFull() {
        commonInitialization();
        showMainFrame();
    }

    static protected void launchTray() {
        commonInitialization();
        validateConnection();
    }

// -----------------------------------------------------------------------------

    static private void clearTempDir() {
        try {
            File dir = Platform.getDefault().getTempDir();
            File files[] = dir.listFiles();
            for (File file : files) {
                file.delete();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    static private void initLookAndFeel() {
        FontManagement.configureUIManager();
        try {
            SyntheticaLookAndFeel.setWindowsDecorated(false);
            UIManager.setLookAndFeel(new SyntheticaStandardLookAndFeel());
            SyntheticaLookAndFeel.setFont(FontManagement.FAMILY, 13);
            SyntheticaLookAndFeel.setAntiAliasEnabled(true);
            SyntheticaLookAndFeel.setExtendedFileChooserEnabled(false);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    static private MainFrame initMainFrame() {
        MainFrame mf = new MainFrame();
        mf.resetTitle();
        mf.setIconImage(UIUtils.getIcon16x16("logo.png").getImage());
        return mf;
    }

    static private TrayIcon initSystemTray() {
        SystemTray tray = SystemTray.getSystemTray();
        TrayIcon icon = new TrayIcon(UIUtils.getIcon16x16("programm_offline.png").getImage());
        try {
            tray.add(icon);
            icon.setImageAutoSize(true);
            icon.setToolTip("Tealsi Tasks System");
            icon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        showMainFrame();
                    }
                }
            });
        } catch (Exception ex) {}
        return icon;
    }

    // -- Main Frame operations

    static boolean validateConnection() {
        return mainFrame.validateConnection();
    }
    
    static void resetMainWindowTitle() {
        mainFrame.resetTitle();
    }

    static void showMainFrame() {
        mainFrame.setVisible(true);
        if (showNewMessageIcon) {
            mainFrame.openTaskFrame();
            showNewMessageIcon = false;
        }
        validateTrayIcon();
    }

    static void hideMainFrame() {
        mainFrame.setVisible(false);
    }

    static void refreshTaskList() {
        mainFrame.refreshTaskList();
    }

    // -- Notification window

    static void showNotificationWindow() {
        notificationWindow.display();
    }

    static void hideNotificationWindow() {
        notificationWindow.setVisible(false);
    }

    static void addNotification(String message) {
        notificationWindow.addMessage(message);
        if (mainFrame != null && mainFrame.isVisible() && mainFrame.isTaskFrameVisible()) {
            showNewMessageIcon = false;
        } else {
            showNewMessageIcon = true;
        }
        validateTrayIcon();
    }

    // -- Validate tray icon status

    static boolean showNewMessageIcon = false;

    static void validateTrayIcon() {
        boolean isOnline = Platform.getDefault().isConnected();
        if (isOnline) {
            if (showNewMessageIcon) {
                trayIcon.setImage(UIUtils.getIcon16x16("animated.gif").getImage());
            } else {
                trayIcon.setImage(UIUtils.getIcon16x16("programm_online.png").getImage());
            }
            trayIcon.setPopupMenu(createOnlineMenu());
        } else {
            trayIcon.setImage(UIUtils.getIcon16x16("programm_offline.png").getImage());
            trayIcon.setPopupMenu(createOfflineMenu());
        }
    }

    // -- Popup menu creation

    static private PopupMenu createOfflineMenu() {
        PopupMenu menu = new PopupMenu();
        menu.add(createExitAction());
        return menu;
    }

    static private PopupMenu createOnlineMenu() {
        PopupMenu menu = new PopupMenu();
        menu.add(createExitAction());
        return menu;
    }

    static private MenuItem createExitAction() {
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        return exitItem;
    }
}
